package om.recorder;


import ohos.media.audio.AudioCapturer;
import ohos.media.audio.AudioCapturerInfo;
import ohos.media.audio.AudioStreamInfo;

/**
 * A {@code Source} represents Audio Record Source.
 *
 * @author Kailash Dabhi
 * @date 01 July, 2017 12:52 AM
 */
interface Source {
    AudioCapturer audioRecord();

    AudioRecordConfig config();

    int minimumBufferSize();

    class Default implements Source {
        private final AudioCapturer audioRecord;
        private final AudioRecordConfig config;
        private final int minimumBufferSize;

        Default(AudioRecordConfig config) {
            this.config = config;
            this.minimumBufferSize = new MinimumBufferSize(config).asInt();

            AudioStreamInfo audioStreamInfo = new AudioStreamInfo.Builder()
                    .encodingFormat(config.audioEncoding()) // 16-bit PCM
                    .channelMask(config.channelPositionMask()) // 双声道输入
                    .sampleRate(config.frequency()) // 44.1kHz
                    .build();

            AudioCapturerInfo audioCapturerInfo = new AudioCapturerInfo.Builder().audioStreamInfo(audioStreamInfo)
                    .audioInputSource(config.audioSource())
                    .build();

            this.audioRecord = new AudioCapturer(audioCapturerInfo);
        }

        @Override
        public AudioCapturer audioRecord() {
            return audioRecord;
        }

        @Override
        public AudioRecordConfig config() {
            return config;
        }

        @Override
        public int minimumBufferSize() {
            return minimumBufferSize;
        }
    }
}
